package org.benjamin.lending.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.benjamin.lending.domain.RechargeForm;
import org.benjamin.lending.domain.trusteeship.RequestParameter;
import org.benjamin.lending.service.TrusteeshipService;
import org.benjamin.lending.utils.CommonsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;


@Controller
@RequestMapping(value = "trusteeship")
public class TrusteeshipController {

	private final Logger logger = LogManager.getLogger();
	
	@Autowired
	private TrusteeshipService trusteeshipService;
	
	@PostMapping(value = "accountManager")
	public String accountManager(ModelMap modelMap){
		try{
			String username = CommonsUtils.getSessionUser();
			RequestParameter parameter = this.trusteeshipService.accountManager(username);
			modelMap.addAttribute("parameter",parameter);
		}catch (Exception e){
			logger.error(e.getMessage(),e);
		}
		return "request/sumpapay";
	}

	@PostMapping(value = "simpleRecharge")
	public String simpleRecharge(BigDecimal amount, ModelMap modelMap, HttpServletRequest request){
		String username = CommonsUtils.getSessionUser();
		logger.info("当前用户【{}】开始请求快速充值通道",username);
		if(amount != null && amount.compareTo(BigDecimal.ZERO) == 1){
			String createIp = request.getRemoteAddr();
			RequestParameter parameter = this.trusteeshipService.simpleRecharge(username,amount,createIp);
			logger.info("页面请求连接：{}，请求参数{}",parameter.getUrl(),parameter.getRequestMap());
			logger.info("------------------结束分割线------------------");
			modelMap.put("parameter",parameter);
		}
		return "request/sumpapay";
	}

	@RequestMapping(value = "bankRecharge")
	public String bankRecharge(RechargeForm rechargeForm, ModelMap modelMap, HttpServletRequest request){
		String username = CommonsUtils.getSessionUser();
		logger.info("当前用户【{}】开始请求网银充值通道，网银编号：{}",username,rechargeForm.getBankCode());
		if(rechargeForm.getAmount() != null && rechargeForm.getAmount().compareTo(BigDecimal.ZERO) == 1){
			String createIp = request.getRemoteAddr();
			RequestParameter parameter = this.trusteeshipService.bankRecharge(username,rechargeForm.getAmount(),rechargeForm.getBankCode(),createIp);
			logger.info("页面请求连接：{}，请求参数{}",parameter.getUrl(),parameter.getRequestMap());
			logger.info("------------------结束分割线------------------");
			modelMap.put("parameter",parameter);
		}else{
			return "redirect:/recharge/bankRecharge";
		}
		return "request/sumpapay";
	}

	@RequestMapping(value = "success")
	public String success(HttpServletRequest request){
		logger.info("------------------同步返回开始分割线------------------");
		Map<String,Object> requestParameters = WebUtils.getParametersStartingWith(request,null);
		logger.info("------------------同步返回结束分割线------------------");
		return "trusteeship.success";
	}

	@ResponseBody
	@RequestMapping(value = "notice")
	public String notice(HttpServletRequest request){
		logger.info("------------------同步返回开始分割线------------------");
		Map<String,Object> requestParameters = WebUtils.getParametersStartingWith(request,null);
		logger.info("异步相应信息：{}",requestParameters);
		String createIp = request.getRemoteAddr();
		this.trusteeshipService.responseAsyn(requestParameters,createIp);
		logger.info("------------------同步返回结束分割线------------------");
		return "success";
	}

}
